#include<iostream>
#include<cstring>
using namespace std;
const int N = 1e4 + 10, M = 5e4 + 10;
int h[N], e[M], ne[M], idx;
int n, m;
int dfn[N], low[N], timestamp;
int scc_cnt, sz[N], stk[N], top;
int id[N], d[N];
bool in_stk[N];
void add(int a, int b)
{
	e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
void tarjan(int u)
{
	dfn[u] = low[u] = ++timestamp;
	stk[++top] = u;
	in_stk[u] = 1;
	for (int i = h[u]; ~i; i = ne[i]) {
		int j = e[i];
		if (!dfn[j]) {
			tarjan(j);
			low[u] = min(low[u], low[j]);
		}
		else if (in_stk[j]) {
			low[u] = min(low[u], dfn[j]);
		}
	}
	if (dfn[u] == low[u]) {
		int y;
		scc_cnt++;
		do {
			y = stk[top--];
			in_stk[u] = 0;
			id[y] = scc_cnt;
			sz[scc_cnt]++;
		} while (y != u);
	}
}
int main()
{
	cin >> n >> m;
	memset(h, -1, sizeof(h));
	while (m--) {
		int a, b;
		cin >> a >> b;
		add(a, b);
	}
	for (int i = 1; i <= n; i++) {
		if (!dfn[i]) {
			tarjan(i);
		}
	}
	int ans = 0, zero = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = h[i]; ~j; j = ne[j]) {
			int k = e[j];
			if (id[i] != id[k]) {
				d[id[i]]++;
			}
		}
	}
	for (int i = 1; i <= scc_cnt; i++) {
		if (!d[i]) {
			zero++;
			ans = sz[i];
			if (zero >= 2) {
				ans = 0;
				break;
			}
		}
	}
	cout << ans << endl;
	return 0;
}